<template>
    <div>
        <!--面包屑导航-->
        <el-breadcrumb separator-class="el-icon-arrow-right">
            <el-breadcrumb-item :to="{ path: '/manhome' }">首页</el-breadcrumb-item>
            <el-breadcrumb-item>文书列表</el-breadcrumb-item>
        </el-breadcrumb>
        <!--用户列表主部分-->
        <el-card>
            <el-row :gutter="25">
                <el-col span="10">
                <!--搜索区--> 
                   <el-input placeholder="输入搜索内容" v-model="queryInfo.query" clearable @clear="getBookList">
                    <el-button slot="append" icon="el-icon-search" @click="getBookList"></el-button>
                   </el-input>
                </el-col>
            <!--添加区-->
                <el-col span="10">
                 <el-button type="primary" @click="addDialogVisible=true">添加</el-button>
                 
                </el-col>
            </el-row>
            <!--表格 border边框 stripe隔行变色-->
            <el-table :data="bookList" border stripe>
                <el-table-column label="文书Id" prop="bid"></el-table-column>
                <el-table-column label="文书名" prop="bname"></el-table-column>
                <el-table-column label="图片" prop="bpicture"></el-table-column>
                <el-table-column label="拥有者" prop="bowner"></el-table-column>
                <el-table-column label="发源地" prop="borigin"></el-table-column>
                <el-table-column label="保密等级" prop="brank"></el-table-column>
                <el-table-column label="内容" prop="btext"></el-table-column>
                <el-table-column label="操作" prop="Action">
           
                 <template slot-scope="scope">
                 <el-button type="primary" icon="el-icon-edit" size="mini" @click="showEditDialog(scope.row)">修改</el-button>
                 <el-button type="danger" icon="el-icon-delete" size="mini" @click="deleteBook(scope.row)">删除</el-button>
                 </template>
                </el-table-column>
            </el-table>
            <!--分页组件-->
            <div>
                <el-pagination
                    @size-change="handleSizeChange"
                    @current-change="handleCurrentChange"
                    :current-page="currentPage4"
                    :page-sizes="[5, 10, 20, 30]"
                    :page-size="queryInfo.pageSize"
                    layout="total, sizes, prev, pager, next, jumper"
                    :total="total">
                </el-pagination>
            </div>
        </el-card>
        <!--查看区域弹窗-->

        <el-dialog title="文书内容" :visible.sync="showDialogVisible" width="50%" @close="showDialogClose">

            <el-form :model="showForm" ref="showFormRef" label-width="70px"></el-form>

            <div style="display: inline; color: #20a0ff;margin-left: 50px;margin-right:20px;font-size: 12px;">

                {{showForm.btext}}

              </div>

        </el-dialog>

        <!--添加区域弹窗-->
        <el-dialog title="添加文书" :visible.sync="addDialogVisible" width="50%" @close="addDialogClose">
            <el-form :model="addForm" :rules="addFormRules" ref="addFormRef" label-width="70px">
                <el-form-item label="文书Id" prop="bid">
                    <el-input v-model="addForm.bid"></el-input>
                </el-form-item>
                <el-form-item label="文书名" prop="bname">
                    <el-input v-model="addForm.bname"></el-input>
                </el-form-item>
                <el-form-item label="图片" prop="bpicture">
                    <el-input v-model="addForm.bpicture"></el-input>
                </el-form-item>
                <el-form-item label="拥有者" prop="bowner">
                    <el-input v-model="addForm.bowner"></el-input>
                </el-form-item>
                <el-form-item label="发源地" prop="borigin">
                    <el-input v-model="addForm.borigin"></el-input>
                </el-form-item>
                <el-form-item label="保密等级" prop="brank">
                    <el-input v-model="addForm.brank"></el-input>
                </el-form-item>
                <el-form-item label="内容" prop="btext">
                    <el-input v-model="addForm.btext"></el-input>
                </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">  
                <el-button @click="addDialogVisible=false">取消</el-button>
                <el-button type="primary" @click="addBook">添加</el-button>
            </span>
        </el-dialog>
        <!--修改区域弹窗-->
        <el-dialog title="修改文书信息" :visible.sync="editDialogVisible" width="50%" @close="editDialogClose">
            <el-form :model="editForm" :rules="editFormRules" ref="editFormRef" label-width="70px">
               <el-form-item label="文书Id" prop="bid">
                    <el-input v-model="editForm.bid"></el-input>
                </el-form-item>
                <el-form-item label="文书名" prop="bname">
                    <el-input v-model="editForm.bname"></el-input>
                </el-form-item>
                <el-form-item label="图片" prop="bpicture">
                    <el-input v-model="editForm.bpicture"></el-input>
                </el-form-item>
                <el-form-item label="拥有者" prop="bowner">
                    <el-input v-model="editForm.bowner"></el-input>
                </el-form-item>
                <el-form-item label="发源地" prop="borigin">
                    <el-input v-model="editForm.borigin"></el-input>
                </el-form-item>
                <el-form-item label="保密等级" prop="brank">
                    <el-input v-model="editForm.brank"></el-input>
                </el-form-item>
                <el-form-item label="内容" prop="btext">
                    <el-input v-model="editForm.btext"></el-input>
                </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">  
                <el-button @click="editDialogVisible=false">取消</el-button>
                <el-button type="primary" @click="editBook">确认修改</el-button>
            </span>
        </el-dialog>
    </div>
</template>
<script>
    export default{
        created(){
           this.getBookList();
        },
        data(){
            return{
                //查询信息实体
                queryInfo:{
                    query:"",
                    pageNum:1,
                    pageSize:5,
                },
                bookList:[],//文书列表
                total:0,
                //对话框状态
                addDialogVisible:false,
                editDialogVisible:false,
                showDialogVisible:false,
                //表单信息
                addForm:{
                    bid:"",
                    bname:"",
                    bpicture:"",
                    bowner:"",
                    borigin:"",
                    brank:"",
                    btext:""
                },
                editForm:{
                    bid:"",
                    bname:"",
                    bpicture:"",
                    bowner:"",
                    borigin:"",
                    brank:"",
                    btext:""
                },
                showForm:{

                    bid:"",

                    bname:"",

                    bpicture:"",

                    bowner:"",

                    borigin:"",

                    brank:"",

                    btext:""

                },
                //表单验证
                addFormRules:{
                     bid: [
                        { required: true, message: '请输入文书Id', trigger: 'blur' },
                        { min: 5, max: 10, message: '长度在 5到 10 个字符', trigger: 'blur' }
                          ],
                     bname: [
                        { required: true, message: '请输入文书名', trigger: 'blur' },
                          ],
                    bpicture: [
                        { required: true, message: '请输入文书图片', trigger: 'blur' },
                          ],
                    bowner: [
                        { required: true, message: '请输入文书拥有者', trigger: 'blur' },
                          ], 
                    borigin: [
                        { required: true, message: '请输入文书发源地', trigger: 'blur' },
                          ],
                    brank: [
                        { required: true, message: '请输入文书保密等级', trigger: 'blur' },
                          ], 
                    btext: [
                        { required: true, message: '请输入文书内容', trigger: 'blur' },
                          ],       
                     },
                editFormRules:{
                    bid: [
                        { required: true, message: '请输入文书Id', trigger: 'blur' },
                          ],
                    bname: [
                        { required: true, message: '请输入文书名', trigger: 'blur' },
                          ],
                    bpicture: [
                        { required: true, message: '请输入文书图片', trigger: 'blur' },
                          ],      
                    bowner: [
                        { required: true, message: '请输入文书拥有者', trigger: 'blur' },
                          ], 
                    borigin: [
                        { required: true, message: '请输入文书发源地', trigger: 'blur' },
                          ],
                    brank: [
                        { required: true, message: '请输入文书保密等级', trigger: 'blur' },
                          ], 
                    btext: [
                        { required: true, message: '请输入文书内容', trigger: 'blur' },
                          ],       
                     },
            }

        },
        methods:{
           async getBookList(){
                const {data:res} =await this.$http.get("allBook",{params:this.queryInfo});
                this.bookList=res.data;//列表数据封装
                this.total=res.numbers;//总数封装
            },
            //最大数
            handleSizeChange(newSize){
                this.queryInfo.pageSize=newSize;
                this.getBookList();
            },
            //pageNum的触发
            handleCurrentChange(newPage){
                this.queryInfo.pageNum=newPage;
                this.getBookList();
            },
            //监听查看操作

            showDialogClose(){

                this.$refs.showFormRef.resetFields();
            },

            //添加
            //监听添加操作
            addDialogClose(){
                this.$refs.addFormRef.resetFields();
            },
            addBook(){
                this.$refs.addFormRef.validate(async valid=>{
                   if(!valid) return;
                   const {data:res}=await this.$http.post("addBook",this.addForm);
                   if(res!="success"){
                       return this.$message.error("添加失败！");
                   }
                   
                   this.$message.success("添加成功！");
                   this.addDialogVisible=false;
                   this.getBookList();
                })
                   /*
                    const {data:res}="ok";
                    if(res=="ok"){
                    this.$message.error();("操作失败！");
                    
                    }else{
                    this.$message.success("操作成功！");
                    this.addDialogVisible=false;
                    }
                }); */     
            },
            //删除
            async deleteBook(did){
               const confirmResult=await this.$confirm('此操作将永久删除，是否继续','提示',{
                    confirmButtonText:'确定',
                    cancleButtonText:'取消',
                    type:'warning'
                }).catch(err=>err)
                if(confirmResult!='confirm'){
                    return this.$message.Info('已取消删除');
                }
                const {data:res}=await this.$http.post("deleteBook",did);
                if(res!="success"){
                    return this.$message.error("删除失败！");
                }
                this.$message.success("删除成功！");
                this.getBookList();
            },
            //修改
            //显示对话框，按钮触发
            async showEditDialog(Id){
                //const {data:res}=await this.$http.get("editBook"+Id);
                //this.editForm=res;//存储查询信息
                this.editForm=Id;
                this.editDialogVisible=true;//开启编辑框
            },
            //监听修改操作
            editDialogClose(){
                this.$refs.editFormRef.resetFields();//关闭窗口，重置信息
            },
            //确认修改
            editBook(){
                this.$refs.editFormRef.validate(async valid=>{
                    if(!valid) return;
                   const {data:res}=await this.$http.post("editBook",this.editForm);
                   if(res!="success"){
                       return this.$message.error("修改失败！");
                  /* }else{
                   this.$message.error("修改成功！");
                   this.editDialogVisible=false;
                   this.getBookList();
                   }
                });
                  
                   const {data:res}="ok";
                    if(res=="ok"){
                    this.$message.error();("修改失败！");
                     */
                    }else{
                    this.$message.success("修改成功！");
                    this.editDialogVisible=false;
                     this.getBookList();
                    }
                });
            },
        }

    }
</script>
<style  scoped>
.el-breadcrumb{
    margin-bottom: 15px;
    font-size: 15px;
}
</style>